//
// Created by liutp on 2022/6/18.
//

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <SDL2/SDL.h>

#include "config.h"
#include "type.h"
#include "util.h"
#include "gui.h"

int main(int argc, char *argv[]) {
#ifdef BUILD_WASM
    fprintf(stdout, "执行WASM版本\n");
#endif
    fprintf(stdout, "Version: %d.%d\n", ffmpeg_wasm_VERSION_MAJOR, ffmpeg_wasm_VERSION_MINOR);

    const char *file_name = "./dream_it_possible.264";
    int error = 0;

//    SDL_Delay(5000);
    Buffer buf;
    error = read_file(file_name, &buf);
    if (error != 0) {
        fprintf(stderr, "读取文件失败 %s\n", file_name);
    }
//    SDL_Delay(5000);

    Stream *s = ffmpeg_init(AV_CODEC_ID_H264);
    if (s == nullptr) {
        fprintf(stderr, "初始化ffmpeg失败\n");
    }

    ffmpeg_input_data(s, buf.data, buf.size);
    while (true) {
        int err = ffmpeg_decode_frame(s);
        if (err != 0) {
            fprintf(stderr, "ffmpeg_input_data > %d\n", err);
            break;
        }
        gui.update(s);
    }
    ffmpeg_dump(s);

    ffmpeg_release(s);
    s = nullptr;

//    while (true) {
//        SDL_Delay(1000);
//    }
    SDL_Event windowEvent;
    while (true) {
        if (SDL_PollEvent(&windowEvent)) {
            if (SDL_QUIT == windowEvent.type) {
                break;
            }
        }
    }

    return 0;
}